<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class picture_fast extends admin
{
    function __construct()
    {
        parent::__construct();
        $this->M = new_html_special_chars(getcache('picture_fast', 'commons'));
        $this->db = pc_base::load_model('picture_fast_model');
        $this->username = param::get_cookie('admin_username');
        $this->db2 = pc_base::load_model('type_model');
        $this->cdb = pc_base::load_model('category_model');
    }

    public function init()
    {
        $where = array('siteid' => $this->get_siteid());
        if ($_GET['typeid'] != '') {
            $where ['typeid'] = intval($_GET['typeid']);
        }
        if ($_GET['catid'] != '') {
            $where ['catid'] = intval($_GET['catid']);
        }
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->db->listinfo($where, $order = 'listorder ASC,pf_id DESC', $page, $pages = '9');
        $pages = $this->db->pages;
        $types = $this->db2->listinfo(array('module' => ROUTE_M, 'siteid' => $this->get_siteid()), $order = 'typeid DESC');
        $types = new_html_special_chars($types);
        $type_arr = array();
        foreach ($types as $typeid => $type) {
            $type_arr[$type['typeid']] = $type['name'];
        }
        $cates = $this->cdb->get_cates($this->get_siteid());
        $cate_arr = array();
        foreach ($cates as $catindex => $cate) {
            $cate_arr[$cate['catid']] = $cate['catname'];
        }
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_fast&a=add\', title:\'' . L('picture_fast_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_fast_add'));
        extract($_GET);
        include $this->admin_tpl('picture_fast_list');
    }

    /*
     *判断标题重复和验证
     */
    public function public_name()
    {
        $picture_fast_title = isset($_GET['picture_fast_name']) && trim($_GET['picture_fast_name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['picture_fast_name'])) : trim($_GET['picture_fast_name'])) : exit('0');

        $pf_id = isset($_GET['pf_id']) && intval($_GET['pf_id']) ? intval($_GET['pf_id']) : '';
        $data = array();
        if ($pf_id) {

            $data = $this->db->get_one(array('pf_id' => $pf_id), 'name');
            if (!empty($data) && $data['name'] == $picture_fast_title) {
                exit('1');
            }
        }
        if ($this->db->get_one(array('name' => $picture_fast_title), 'pf_id')) {
            exit('0');
        } else {
            exit('1');
        }
    }

    //添加分类时，验证分类名是否已存在
    public function public_check_name()
    {
        $type_name = isset($_GET['type_name']) && trim($_GET['type_name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['type_name'])) : trim($_GET['type_name'])) : exit('0');
        $type_name = safe_replace($type_name);
        $typeid = isset($_GET['typeid']) && intval($_GET['typeid']) ? intval($_GET['typeid']) : '';
        $data = array();
        if ($typeid) {
            $data = $this->db2->get_one(array('typeid' => $typeid), 'name');
            if (!empty($data) && $data['name'] == $type_name) {
                exit('1');
            }
        }
        if ($this->db2->get_one(array('name' => $type_name), 'typeid')) {
            exit('0');
        } else {
            exit('1');
        }
    }

    //添加图片速读
    public function add()
    {
        if (isset($_POST['dosubmit'])) {
            $_POST['picture_fast']['inputtime'] = SYS_TIME;
            $_POST['picture_fast']['siteid'] = $this->get_siteid();
            if (empty($_POST['picture_fast']['title'])) {
                showmessage(L('fast_title'), HTTP_REFERER);
            } else {
                $_POST['picture_fast']['title'] = safe_replace($_POST['picture_fast']['title']);
            }
            $data = new_addslashes($_POST['picture_fast']);
            $pf_id = $this->db->insert($data, true);
            if (!$pf_id) return FALSE;
            $siteid = $this->get_siteid();
            //url生成
            $m_db = pc_base::load_model('module_model');
            $data = $m_db->select(array('module' => 'picture_fast'));
            $setting = string2array($data[0]['setting']);
            $set = $setting[$siteid];
            $show_ruleid = $set['show_ruleid'];
            $u_db = pc_base::load_model('urlrule_model');
            $urlrule_arr = $u_db->get_one("`urlruleid`='$show_ruleid'", 'urlrule');
            $urlrule = sizeof($urlrule_arr) > 0 ? $urlrule_arr[0]['urlrule'] : 'index.php?m=picture_fast&c=index&a=show&pf_id={$id}';

            $url = str_replace(array('{$id}'), array($pf_id), $urlrule);
            if (strpos($url, '\\') !== false) {
                $url = APP_PATH . str_replace('\\', '/', $url);
            } else {
                $url = APP_PATH . $url;
            }
            $this->db->update("`url`='$url'", array('pf_id' => $pf_id));
            //更新附件状态
            if (pc_base::load_config('system', 'attachment_stat') & $_POST['picture_fast']['thumb']) {
                $this->attachment_db = pc_base::load_model('attachment_model');
                $this->attachment_db->api_update($_POST['picture_fast']['thumb'], 'picture_fast-' . $pf_id, 1);
            }
            showmessage(L('operation_success'), HTTP_REFERER, '', 'add');
        } else {
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);
            $siteid = $this->get_siteid();
            $types = $this->db2->get_types($siteid);
            $cates = $this->cdb->get_cates($siteid);

            $template_list = template_list($siteid, 0);
            $site = pc_base::load_app_class('sites', 'admin');
            $info = $site->get_by_id($siteid);
            foreach ($template_list as $k => $v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }

            //print_r($types);exit;
            include $this->admin_tpl('picture_fast_add');
        }

    }

    /**
     * 说明:异步更新排序
     * @param  $optionid
     */
    public function listorder_up()
    {
        $result = $this->db->update(array('listorder' => '+=1'), array('pf_id' => $_GET['pf_id']));
        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
    }

    //更新排序
    public function listorder()
    {
        if (isset($_POST['dosubmit'])) {
            foreach ($_POST['listorders'] as $pf_id => $listorder) {
                $pf_id = intval($pf_id);
                $this->db->update(array('listorder' => $listorder), array('pf_id' => $pf_id));
            }
            showmessage(L('operation_success'), HTTP_REFERER);
        }
    }

    //添加图片速读分类
    public function add_type()
    {
        if (isset($_POST['dosubmit'])) {
            if (empty($_POST['type']['name'])) {
                showmessage(L('typename_noempty'), HTTP_REFERER);
            }
            $_POST['type']['siteid'] = $this->get_siteid();
            $_POST['type']['module'] = ROUTE_M;
            $this->db2 = pc_base::load_model('type_model');
            $typeid = $this->db2->insert($_POST['type'], true);
            if (!$typeid) return FALSE;
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            $show_validator = $show_scroll = true;
            $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_fast&a=add\', title:\'' . L('picture_fast_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_fast_add'));
            include $this->admin_tpl('picture_fast_type_add');
        }

    }

    /**
     * 删除分类
     */
    public function delete_type()
    {
        if ((!isset($_GET['typeid']) || empty($_GET['typeid'])) && (!isset($_POST['typeid']) || empty($_POST['typeid']))) {
            showmessage(L('illegal_parameters'), HTTP_REFERER);
        } else {
            if (is_array($_POST['typeid'])) {
                foreach ($_POST['typeid'] as $typeid_arr) {
                    $this->db2->delete(array('typeid' => $typeid_arr));
                }
                showmessage(L('operation_success'), HTTP_REFERER);
            } else {
                $typeid = intval($_GET['typeid']);
                if ($typeid < 1) return false;
                $result = $this->db2->delete(array('typeid' => $typeid));
                if ($result) {
                    showmessage(L('operation_success'), HTTP_REFERER);
                } else {
                    showmessage(L("operation_failure"), HTTP_REFERER);
                }
            }
        }
    }

    //:分类管理
    public function list_type()
    {
        $this->db2 = pc_base::load_model('type_model');
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->db2->listinfo(array('module' => ROUTE_M, 'siteid' => $this->get_siteid()), $order = 'listorder DESC', $page, $pages = '10');
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_fast&a=add\', title:\'' . L('picture_fast_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_fast_add'));
        $pages = $this->db2->pages;
        include $this->admin_tpl('picture_fast_list_type');
    }

    public function edit()
    {
        if (isset($_POST['dosubmit'])) {
            $pf_id = intval($_GET['pf_id']);
            if ($pf_id < 1) return false;
            if (!is_array($_POST['picture_fast']) || empty($_POST['picture_fast'])) return false;
            if ((!$_POST['picture_fast']['title']) || empty($_POST['picture_fast']['title'])) return false;
            $this->db->update($_POST['picture_fast'], array('pf_id' => $pf_id));
            //更新附件状态
            if (pc_base::load_config('system', 'attachment_stat') & $_POST['picture_fast']['thumb']) {
                $this->attachment_db = pc_base::load_model('attachment_model');
                $this->attachment_db->api_update($_POST['picture_fast']['thumb'], 'picture_fast-' . $pf_id, 1);
            }
            showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast&a=edit', '', 'edit');

        } else {
            $siteid = $this->get_siteid();
            $show_validator = $show_scroll = $show_header = true;
            pc_base::load_sys_class('form', '', 0);
            $types = $this->db2->listinfo(array('module' => ROUTE_M, 'siteid' => $this->get_siteid()), $order = 'typeid DESC');
            $type_arr = array();
            foreach ($types as $typeid => $type) {
                $type_arr[$type['typeid']] = $type['name'];
            }
            $cates = $this->cdb->get_cates($this->get_siteid());
            $cate_arr = array();
            foreach ($cates as $typeid => $cate) {
                $cate_arr[$cate['catid']] = $cate['catname'];
            }

            $template_list = template_list($siteid, 0);
            foreach ($template_list as $k => $v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }

            //解出链接内容
            $info = $this->db->get_one(array('pf_id' => $_GET['pf_id']));
            if (!$info) showmessage(L('picture_fast_exit'));
            extract($info);
            include $this->admin_tpl('picture_fast_edit');
        }

    }

    /**
     * 修改图片速读 分类
     */
    public function edit_type()
    {
        if (isset($_POST['dosubmit'])) {
            $typeid = intval($_GET['typeid']);
            if ($typeid < 1) return false;
            if (!is_array($_POST['type']) || empty($_POST['type'])) return false;
            if ((!$_POST['type']['name']) || empty($_POST['type']['name'])) return false;
            $this->db2->update($_POST['type'], array('typeid' => $typeid));
            showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast&a=list_type', '', 'edit');

        } else {
            $show_validator = $show_scroll = $show_header = true;
            //解出分类内容
            $info = $this->db2->get_one(array('typeid' => $_GET['typeid']));
            if (!$info) showmessage(L('picture_fasttype_exit'));
            extract($info);
            include $this->admin_tpl('picture_fast_type_edit');
        }

    }

    /**
     * 删除图片速读
     * @param intval $sid 图片速读ID，递归删除
     */
    public function delete()
    {
        if ((!isset($_GET['pf_id']) || empty($_GET['pf_id'])) && (!isset($_POST['pf_id']) || empty($_POST['pf_id']))) {
            showmessage(L('illegal_parameters'), HTTP_REFERER);
        } else {
            if (is_array($_POST['pf_id'])) {
                foreach ($_POST['pf_id'] as $pf_id_arr) {
                    //批量删除图片速读
                    $this->db->delete(array('pf_id' => $pf_id_arr));
                    //更新附件状态
                    if (pc_base::load_config('system', 'attachment_stat')) {
                        $this->attachment_db = pc_base::load_model('attachment_model');
                        $this->attachment_db->api_delete('picture_fast-' . $pf_id_arr);
                    }
                }
                showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast');
            } else {
                $pf_id = intval($_GET['pf_id']);
                if ($pf_id < 1) return false;
                //删除图片速读
                $result = $this->db->delete(array('pf_id' => $pf_id));
                //更新附件状态
                if (pc_base::load_config('system', 'attachment_stat')) {
                    $this->attachment_db = pc_base::load_model('attachment_model');
                    $this->attachment_db->api_delete('picture_fast-' . $pf_id);
                }
                if ($result) {
                    showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast');
                } else {
                    showmessage(L("operation_failure"), '?m=picture_fast&c=picture_fast');
                }
            }
            showmessage(L('operation_success'), HTTP_REFERER);
        }
    }

    /**
     * 投票模块配置
     */
    public function setting()
    {
        //读取配置文件
        $data = array();
        $siteid = $this->get_siteid();//当前站点
        //更新模型数据库,重设setting 数据.
        $m_db = pc_base::load_model('module_model');
        $data = $m_db->select(array('module' => 'picture_fast'));
        $setting = string2array($data[0]['setting']);
        $now_seting = $setting[$siteid]; //当前站点配置
        if (isset($_POST['dosubmit'])) {
            //多站点存储配置文件
            $setting[$siteid] = $_POST['setting'];
            setcache('picture_fast', $setting, 'commons');
            //更新模型数据库,重设setting 数据.
            $m_db = pc_base::load_model('module_model'); //调用模块数据模型
            $set = array2string($setting);
            $m_db->update(array('setting' => $set), array('module' => ROUTE_M));
            showmessage(L('setting_updates_successful'), '?m=picture_fast&c=picture_fast&a=init');
        } else {
            pc_base::load_sys_class('form', '', 0);
            @extract($now_seting);
            $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_fast&a=add\', title:\'' . L('picture_fast_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_fast_add'));
            include $this->admin_tpl('setting');
        }
    }

    //批量审核申请 ...
    public function check_register()
    {
        if (isset($_POST['dosubmit'])) {
            if ((!isset($_GET['pf_id']) || empty($_GET['pf_id'])) && (!isset($_POST['pf_id']) || empty($_POST['pf_id']))) {
                showmessage(L('illegal_parameters'), HTTP_REFERER);
            } else {
                if (is_array($_POST['pf_id'])) {//批量审核
                    foreach ($_POST['pf_id'] as $pf_id_arr) {
                        $this->db->update(array('passed' => 1), array('pf_id' => $pf_id_arr));
                    }
                    showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast');
                } else {//单个审核
                    $pf_id = intval($_GET['pf_id']);
                    if ($pf_id < 1) return false;
                    $result = $this->db->update(array('passed' => 1), array('pf_id' => $pf_id));
                    if ($result) {
                        showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast');
                    } else {
                        showmessage(L("operation_failure"), '?m=picture_fast&c=picture_fast');
                    }
                }
            }
        } else {//读取未审核列表
            $where = array('siteid' => $this->get_siteid(), 'passed' => 0);
            $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
            $infos = $this->db->listinfo($where, 'pf_id DESC', $page, $pages = '9');
            $pages = $this->db->pages;
            $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=picture_fast&c=picture_fast&a=add\', title:\'' . L('picture_fast_add') . '\', width:\'700\', height:\'450\'}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('picture_fast_add'));
            include $this->admin_tpl('check_register_list');
        }

    }

    //单个审核申请
    public function check()
    {
        if ((!isset($_GET['pf_id']) || empty($_GET['pf_id'])) && (!isset($_POST['pf_id']) || empty($_POST['pf_id']))) {
            showmessage(L('illegal_parameters'), HTTP_REFERER);
        } else {
            $pf_id = intval($_GET['pf_id']);
            if ($pf_id < 1) return false;
            //删除图片速读
            $result = $this->db->update(array('passed' => 1), array('pf_id' => $pf_id));
            if ($result) {
                showmessage(L('operation_success'), '?m=picture_fast&c=picture_fast');
            } else {
                showmessage(L("operation_failure"), '?m=picture_fast&c=picture_fast');
            }

        }
    }


    /**
     * 说明:对字符串进行处理
     * @param $string 待处理的字符串
     * @param $isjs 是否生成JS代码
     */
    function format_js($string, $isjs = 1)
    {
        $string = addslashes(str_replace(array("\r", "\n"), array('', ''), $string));
        return $isjs ? 'document.write("' . $string . '");' : $string;
    }


}

?>